/* * @Author: silas * @Date: 2020-11-26 11:10:46 * @Last Modified by: silas *
@Last Modified time: 2020-11-26 14:51:12 */
<template>
  <div class="g-wrapper">
    <header>
      <div class="title">云网-Top清单</div>
    </header>
    <main>
      <el-form :inline="true" :model="form" class="main_searchBar" ref="form">
        <el-form-item label="区域:">
          <dx-cascader :area_id.sync="form.areaId" ref="cascader" />
        </el-form-item>
        <el-form-item label="收入指标:">
          <el-select size="mini" v-model="form.code" clearable>
            <el-option v-for="(item, index) in codeList" :key="index" :label="item.indexName" :value="item.indexCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户群:">
          <el-select size="mini" v-model="form.customer" clearable>
            <el-option v-for="(item, index) in customerList" :key="index" :label="item.indexName" :value="item.indexCode"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账期:">
          <el-date-picker size="mini" v-model="form.accountPeriod" value-format="yyyyMM" type="month" placeholder="选择月"></el-date-picker>
        </el-form-item>
        <el-form-item label="Top:">
          <el-select size="mini" v-model="form.pageSize" clearable>
            <el-option v-for="(item, index) in pageSizeList" :key="index" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产/客户:">
          <el-select size="mini" v-model="form.assetType" clearable>
            <el-option v-for="(item, index) in assetTypeList" :key="index" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" id="search_btn" type="primary" @click="handle_search">查询</el-button>
          <el-button size="mini" id="export_btn" type="orange" @click="handle_export">导出</el-button>
        </el-form-item>
      </el-form>
      
      <div class="tables">
        <el-tabs v-model="tabActive" type="card" style="margin-top: 10px;">
          <el-tab-pane label="本期新增TOP资产" name="list1"></el-tab-pane>
          <el-tab-pane label="本期流失TOP资产" name="list2"></el-tab-pane>
          <el-tab-pane label="存量上涨波动" name="list3"></el-tab-pane>
          <el-tab-pane label="存量下降波动" name="list6"></el-tab-pane>
          <el-tab-pane label="存量转入" name="list4"></el-tab-pane>
          <el-tab-pane label="存量转出" name="list5"></el-tab-pane>
        </el-tabs>
        
        <div v-show="tabActive=='list1'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table1"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData1"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList1" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list2'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table2"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData2"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList2" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list3'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table3"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData3"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList3" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list4'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table4"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData4"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList4" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list5'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table5"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData5"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList5" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
        <div v-show="tabActive=='list6'">
            <p class="g-unit">单位: 元</p>
          <el-table
            :max-height="height+'px'"
            id="table6"
            size="mini"
            :header-cell-style="{ background: '#eef1f6' }"
            :data="tableData5"
            v-loading="tableLoading"
            style="width: 100%;"
            border
          >
            <el-table-column align="center" v-for="(item, index) in titleList6" :key="index" :label="item.name" :prop="item.prop"></el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
// 引入导出Excel表格依赖
import FileSaver from 'file-saver';
import XLSX from 'xlsx';
import { $budgetAPI } from '@/api';
import { setSessionStorage, getSessionStorage, removeSessionStorage } from '@/common/js/storage';
export default {
    data() {
        return {
            form: {
                areaId: '',
                code: '',
                customer: '',
                accountPeriod: '',
                pageSize: 10,
                assetType: 1
            },
            pageSizeList: [
                {
                    name: 10,
                    code: 10
                },
                {
                    name: 20,
                    code: 20
                },
                {
                    name: 50,
                    code: 50
                },
                {
                    name: 100,
                    code: 100
                }
            ],
            assetTypeList: [
                {
                    name: '资产',
                    value: 1
                },
                {
                    name: '客户',
                    value: 2
                }
            ],
            tabActive: 'list1',
            pickerOptions: this.pickerDisabled(),
            customerList: [
                {
                    indexName: '全部',
                    indexCode: ''
                },
                {
                    indexName: '政企',
                    indexCode: '政企'
                },
                {
                    indexName: '公众',
                    indexCode: '公众'
                }
            ],
            tableData1: [],
            tableData2: [],
            tableData3: [],
            tableData4: [],
            tableData5: [],
            tableData6: [],
            titleList1: [
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                },
                {
                    name: '本期收入',
                    prop: 'currentIncome'
                },
                {
                    name: 'TOP排名',
                    prop: 'topRanking'
                }
            ],
            titleList2: [
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                },
                {
                    name: '上期收入',
                    prop: 'uPcurrentIncome'
                },
                {
                    name: 'TOP排名',
                    prop: 'topRanking'
                }
            ],
            titleList3: [
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '号码',
                    prop: 'numberp'
                },
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '本期收入',
                    prop: 'currentIncome'
                },
                {
                    name: '上期收入',
                    prop: 'uPcurrentIncome'
                },
                {
                    name: '环比波动',
                    prop: 'fluctuation'
                },
                {
                    name: 'TOP编号',
                    prop: 'topno'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                }
            ],
            titleList4: [
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '上期支局ID',
                    prop: 'oldbranchId'
                },
                {
                    name: '号码',
                    prop: 'numberp'
                },
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '本期收入',
                    prop: 'currentIncome'
                },
                {
                    name: '上期收入',
                    prop: 'uPcurrentIncome'
                },
                {
                    name: '环比波动',
                    prop: 'fluctuation'
                },
                {
                    name: 'TOP编号',
                    prop: 'topno'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                }
            ],
            titleList5: [
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '上期支局ID',
                    prop: 'oldbranchId'
                },
                {
                    name: '上期支局名称',
                    prop: 'oldbranchName'
                },
                {
                    name: '号码',
                    prop: 'numberp'
                },
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '本期收入',
                    prop: 'currentIncome'
                },
                {
                    name: '上期收入',
                    prop: 'uPcurrentIncome'
                },
                {
                    name: '环比波动',
                    prop: 'fluctuation'
                },
                {
                    name: 'TOP编号',
                    prop: 'topno'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                }
            ],
            titleList6: [
                {
                    name: '县分',
                    prop: 'countyBranch'
                },
                {
                    name: '县分名称',
                    prop: 'nameOfCountyBranch'
                },
                {
                    name: '支局ID',
                    prop: 'branchId'
                },
                {
                    name: '支局名称',
                    prop: 'branchName'
                },
                {
                    name: '号码',
                    prop: 'numberp'
                },
                {
                    name: '客户编码',
                    prop: 'customerCode'
                },
                {
                    name: '客户名称',
                    prop: 'customerName'
                },
                {
                    name: '产品类型',
                    prop: 'productType'
                },
                {
                    name: '本期收入',
                    prop: 'currentIncome'
                },
                {
                    name: '上期收入',
                    prop: 'uPcurrentIncome'
                },
                {
                    name: '环比波动',
                    prop: 'fluctuation'
                },
                {
                    name: 'TOP编号',
                    prop: 'topno'
                },
                {
                    name: '客户经理ID',
                    prop: 'accountManagerId'
                },
                {
                    name: '客户经理名称',
                    prop: 'nameOfAccountManager'
                }
            ],
            codeList: [],
            tableLoading: true,
            disabledTime: '',
            height: ''
            // currentPage:1,
        };
    },
    created() {
        window.addEventListener('resize', this.getHeight);
    },
    destroyed() {
        window.removeEventListener('resize', this.getHeight);
    },
    mounted() {
        let that = this;
        // 获取账户信息
        that.accountInfo = JSON.parse(getSessionStorage('accountInfo'));
        // 初始化数据
        that.initData();
        that.getHeight();
    },
    methods: {
        getHeight() {
            this.height = window.innerHeight - this.$refs.form.$el.offsetHeight - 83 - 54 - 51 - 40;
        },
        // 设置月份disabled
        pickerDisabled() {
            let self = this;
            return {
                disabledDate(time) {
                    if (self.disabledTime) {
                        let timeyear = time.getFullYear();
                        let timemonth = time.getMonth() + 1;
                        if (timemonth >= 1 && timemonth <= 9) {
                            timemonth = '0' + timemonth;
                        }
                        let currentdate = timeyear.toString() + timemonth.toString();
                        if (currentdate > self.disabledTime) {
                            return true;
                        }
                    }
                }
            };
        },
        // 搜索
        handle_search() {
            let that = this;
            that.getData();
        },
        // 导出
        handle_export() {
            let that = this;
            let workbook = XLSX.utils.book_new();
            let ws1 = XLSX.utils.table_to_sheet(document.querySelector('#table1'));
            XLSX.utils.book_append_sheet(workbook, ws1, '本期新增TOP资产');
            let ws2 = XLSX.utils.table_to_sheet(document.querySelector('#table2'));
            XLSX.utils.book_append_sheet(workbook, ws2, '本期流失TOP资产');
            let ws3 = XLSX.utils.table_to_sheet(document.querySelector('#table3'));
            XLSX.utils.book_append_sheet(workbook, ws3, '存量上涨波动');
            let ws4 = XLSX.utils.table_to_sheet(document.querySelector('#table4'));
            XLSX.utils.book_append_sheet(workbook, ws4, '存量转入');
            let ws5 = XLSX.utils.table_to_sheet(document.querySelector('#table5'));
            XLSX.utils.book_append_sheet(workbook, ws5, '存量转出');
            let ws6 = XLSX.utils.table_to_sheet(document.querySelector('#table6'));
            XLSX.utils.book_append_sheet(workbook, ws6, '存量下降波动');
            let wbout = XLSX.write(workbook, {
                bookType: 'xlsx',
                bookSST: true,
                type: 'array'
            });
            try {
                FileSaver.saveAs(
                    new Blob([wbout], {
                        type: 'application/octet-stream'
                    }),
                    '波动.xlsx'
                );
            } catch (e) {
                if (typeof console !== 'undefined') console.log(e, wbOut);
            }
            return wbout;
        },
        // 返回
        handle_back() {
            this.$router.go(-1);
        },
        // 初始化数据
        async initData() {
            let that = this,
                accountPeriod,
                code,
                customer,
                obj = {};
            code = await that.getCodeList();
            // customer = await that.getCustomerGroup();
            accountPeriod = await that.getMonth();
            that.codeList = code;
            // that.customerList = customer;
            that.form.accountPeriod = accountPeriod;
            that.disabledTime = accountPeriod;
            that.form.areaId = that.accountInfo.areaId;
            that.form.code = that.$route.query.code || code[0].indexCode;
            that.form.customer = that.$route.query.customer || '';
            that.getData();
        },
        // 获取月份
        getMonth() {
            return new Promise(resolve => {
                $budgetAPI.getCloudMaxMonth(
                    {
                        areaId: this.accountInfo.areaId,
                        areaLvl:this.accountInfo.areaLvl
                    },
                    res => {
                        if (res.code == 0) {
                            resolve(res.content);
                        }
                    },
                    err => {
                        console.log(err);
                    }
                );
            });
        },
        getCodeList() {
            let that = this;

            return new Promise(resolve => {
                $budgetAPI.getWaveIndexCodeList(
                    {},
                    res => {
                        if (res && res.code == 0) {
                            resolve(res.content);
                        }
                    },
                    err => {}
                );
            });
        },
        // getCustomerGroup() {
        //     let that = this;

        //     return new Promise(resolve => {
        //         $budgetAPI.getCustomerGroup(
        //             {},
        //             res => {
        //                 if (res && res.code == 0) {
        //                     resolve(res.content);
        //                 }
        //             },
        //             err => {}
        //         );
        //     });
        // },
        // 获取data
        getData() {
            let that = this;
            let indexName = '';
            that.codeList.map(item => {
                if (item.indexCode == that.form.code) {
                    indexName = item.indexName;
                }
            });
            let obj = {
                accountPeriod: that.form.accountPeriod,
                areaId: that.form.areaId,
                customer: that.form.customer,
                code: that.form.code,
                pageSize: that.form.pageSize,
                assetType: that.form.assetType,
                indexName: indexName
            };
            that.tableLoading = true;
            $budgetAPI.waveTOPN(
                {
                    bilMonth: obj.accountPeriod, //账期
                    areaId: obj.areaId, //	区域id
                    // indexName: obj.customer,
                    indexCode: obj.code, //指标code
                    indexName: obj.indexName, //指标name
                    custGroup: obj.customer,
                    pageSize: obj.pageSize,
                    assetType: obj.assetType
                },
                res => {
                    that.tableLoading = false;
                    let list1 = res.content.content.data.thisIssueList;
                    let list2 = res.content.content.data.thisIssueList1;
                    let list3 = res.content.content.data.thisIssueList2;
                    let list4 = res.content.content.data.thisIssueList3;
                    let list5 = res.content.content.data.thisIssueList4;
                    let list6 = res.content.content.data.thisIssueList5;
                    let listObj = {
                        list1: list1,
                        list2: list2,
                        list3: list3,
                        list4: list4,
                        list5: list5,
                        list6: list6
                    };
                    that.tableData1 = listObj.list1;
                    that.tableData2 = listObj.list2;
                    that.tableData3 = listObj.list3;
                    that.tableData4 = listObj.list4;
                    that.tableData5 = listObj.list5;
                    that.tableData6 = listObj.list6;
                },
                err => {
                    console.log(err);
                }
            );
        }
    }
};
</script>

<style lang="scss" scoped>
.g-wrapper {
    & > header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eaeaea;
        height: 40px;
        margin: 0 -20px;
        padding: 0 20px;
        .title {
            display: inline-block;
            vertical-align: middle;
            border-left: 2px solid #3587e3;
            padding-left: 10px;
            height: 18px;
            line-height: 18px;
            font-size: 14px;
            font-weight: bold;
        }
    }
    & > main {
        padding: 0 10px 10px;
        .main_searchBar {
            border-bottom: 1px solid #d6d9e5;
            /deep/ .el-form-item {
                margin: 6px 10px 6px 0;
            }
            button {
                color: #fff;
                border: none;
                outline: none;
                &:hover {
                    opacity: 0.8;
                }
            }
            // #export_btn {
            //     background-color: #2cd531;
            // }
        }
        .main_navBar {
            padding: 10px 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .area {
                text-align: left;
                display: flex;
                .it {
                    cursor: pointer;
                    font-size: 14px;
                    display: inline-block;
                    .td {
                        font-weight: bold;
                    }
                    &:last-child {
                        span {
                            &:last-child {
                                display: none;
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>
